package tree.递归;

import po.TreeNode;

import java.util.*;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 二叉树的层序遍历II107.java
 * @Description TODO https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/
 * @createTime 2021年03月30日 12:45:00
 */
public class 二叉树的层序遍历II107 {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        Deque<List<Integer>> deque=new LinkedList();
        if (root==null){
            return new ArrayList<>(deque);
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            List<Integer> list=new ArrayList<>(size);
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                list.add(node.val);
                if (node.left!=null){
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
            deque.offerFirst(list);
        }
        return new ArrayList<>(deque);
    }
}
